1154D - Walking Robot - CodeForces Solution


greedy *1500

Please click on ads to support us..

C++ Code:

#include <bits/stdc++.h>
using namespace std;

#define ff first
#define ss second
#define endl "\n"
#define pb push_back
#define ppb pop_back
#define int long long
#define inf 1e16
#define mod 1000000007
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()
#define fill(a,b) memset(a,b,sizeof(a))
void solve()
{
    int n,b,a;
    cin>>n>>b>>a;
    vector<int>s(n);
    for(int i=0;i<n;i++)cin>>s[i];
    int aa=a,bb=b;
    for(int i=0;i<n;i++)
    {
        if(s[i]==1)
        {
            if(aa<a)
            {
                if(bb)bb--,aa++;
                else aa--;
            }
            else
            {
                if(aa)aa--;
                else bb--;
            }
        }
        else
        {
            if(aa)aa--;
            else bb--;
        }
        if(aa<0 || bb<0)
        {
            cout<<i<<endl;
            return;
        }
    }
    cout<<n<<endl;
}
int32_t main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int T=1;//cin>>T;
    while(T--)solve();
    return 0;
}


Comments

Submit
0 Comments
More Questions

1631B - Fun with Even Subarrays
727A - Transformation from A to B
822B - Crossword solving
1623A - Robot Cleaner
884B - Japanese Crosswords Strike Back
862B - Mahmoud and Ehab and the bipartiteness
429A - Xor-tree
1675C - Detective Task
950A - Left-handers Right-handers and Ambidexters
672B - Different is Good
1C - Ancient Berland Circus
721A - One-dimensional Japanese Crossword
1715B - Beautiful Array
60B - Serial Time
453A - Little Pony and Expected Maximum
1715A - Crossmarket
1715C - Monoblock
1512C - A-B Palindrome
1679B - Stone Age Problem
402A - Nuts
792A - New Bus Route
221A - Little Elephant and Function
492C - Vanya and Exams
1369B - AccurateLee
892B - Wrath
999A - Mishka and Contest
727C - Guess the Array
1625C - Road Optimization
1715D - 2+ doors
267A - Subtractions